<?php

namespace App\Admin\Controllers;

use App\Admin\Components\SettingForm;
use App\Http\Controllers\Controller;
use App\Models\Setting;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Support\JavaScript;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Support\Facades\DB;


class SettingController extends Controller
{

    protected $setting = [];

    public function __construct()
    {
        $this->setting = Setting::get();
    }

    public function index(Content $content)
    {
        if (request()->getMethod() == 'POST') {

            $payloads = request()->except(['_token', '_method']);
            DB::beginTransaction();
            try {
                foreach ($payloads as $key => $value) {
                    if ($key == 'bank') {
                        continue;
                    }
                    Setting::set($key, $value);
                }

                DB::commit();
                return response()->json([
                    'code' => 200,
                    'message' => '修改成功'
                ]);
            } catch (\Exception $exception) {
                DB::rollBack();
                return response()->json([
                    'code' => 400,
                    'message' => $exception->getMessage(),
                ]);
            }
        }
        $content->row(function (Row $row) {
            $type = request('_t', 1);
            $tab = new Tab();
            switch ($type) {
                case 2:
                    $tab->addLink('APP基础设置', request()->fullUrlWithQuery(['_t' => 1]));
                    $tab->add('提现设置', $this->withdraws(), true);
                    $tab->addLink('推广设置', request()->fullUrlWithQuery(['_t' => 3]));
                    $tab->addLink('团长设置', request()->fullUrlWithQuery(['_t' => 6]));
                    //$tab->addLink('OEM账户', request()->fullUrlWithQuery(['_t' => 4]));
                    $tab->addLink('佣金设置', request()->fullUrlWithQuery(['_t' => 7]));
                    $tab->addLink('用户协议/隐私政策', request()->fullUrlWithQuery(['_t' => 5]));
                    break;
                case 3:
                    $tab->addLink('APP基础设置', request()->fullUrlWithQuery(['_t' => 1]));
                    $tab->addLink('提现设置', request()->fullUrlWithQuery(['_t' => 2]));
                    $tab->add('推广设置', $this->invites(), true);
                    $tab->addLink('团长设置', request()->fullUrlWithQuery(['_t' => 6]));
                    //$tab->addLink('OEM账户', request()->fullUrlWithQuery(['_t' => 4]));
                    $tab->addLink('佣金设置', request()->fullUrlWithQuery(['_t' => 7]));
                    $tab->addLink('用户协议/隐私政策', request()->fullUrlWithQuery(['_t' => 5]));
                    break;
                case 4:
                    $tab->addLink('APP基础设置', request()->fullUrlWithQuery(['_t' => 1]));
                    $tab->addLink('提现设置', request()->fullUrlWithQuery(['_t' => 2]));
                    $tab->addLink('推广设置', request()->fullUrlWithQuery(['_t' => 3]));
                    $tab->addLink('团长设置', request()->fullUrlWithQuery(['_t' => 6]));
                    //$tab->add('OEM账户', $this->payments(), true);
                    $tab->addLink('佣金设置', request()->fullUrlWithQuery(['_t' => 7]));
                    $tab->addLink('用户协议/隐私政策', request()->fullUrlWithQuery(['_t' => 5]));
                    break;
                case 5:
                    $tab->addLink('APP基础设置', request()->fullUrlWithQuery(['_t' => 1]));
                    $tab->addLink('提现设置', request()->fullUrlWithQuery(['_t' => 2]));
                    $tab->addLink('推广设置', request()->fullUrlWithQuery(['_t' => 3]));
                    $tab->addLink('团长设置', request()->fullUrlWithQuery(['_t' => 6]));
                    //$tab->addLink('OEM账户', request()->fullUrlWithQuery(['_t' => 4]));
                    $tab->addLink('佣金设置', request()->fullUrlWithQuery(['_t' => 7]));
                    $tab->add('用户协议/隐私政策', $this->privices(), true);
                    break;
                case 6:
                    $tab->addLink('APP基础设置', request()->fullUrlWithQuery(['_t' => 1]));
                    $tab->addLink('提现设置', request()->fullUrlWithQuery(['_t' => 2]));
                    $tab->addLink('推广设置', request()->fullUrlWithQuery(['_t' => 3]));
                    $tab->add('团长设置', $this->group(), true);
                    //$tab->addLink('OEM账户', request()->fullUrlWithQuery(['_t' => 4]));
                    $tab->addLink('佣金设置', request()->fullUrlWithQuery(['_t' => 7]));
                    $tab->addLink('用户协议/隐私政策', request()->fullUrlWithQuery(['_t' => 5]));
                    break;
                case 7:
                    $tab->addLink('APP基础设置', request()->fullUrlWithQuery(['_t' => 1]));
                    $tab->addLink('提现设置', request()->fullUrlWithQuery(['_t' => 2]));
                    $tab->addLink('推广设置', request()->fullUrlWithQuery(['_t' => 3]));
                    $tab->addLink('团长设置', request()->fullUrlWithQuery(['_t' => 6]));
                    //$tab->addLink('OEM账户', request()->fullUrlWithQuery(['_t' => 4]));
                    $tab->add('佣金设置', $this->commissions(), true);
                    $tab->addLink('用户协议/隐私政策', request()->fullUrlWithQuery(['_t' => 5]));
                    break;
                case 1:
                default:
                    $tab->add('APP基础设置', $this->basic());
                    $tab->addLink('提现设置', request()->fullUrlWithQuery(['_t' => 2]));
                    $tab->addLink('推广设置', request()->fullUrlWithQuery(['_t' => 3]));
                    $tab->addLink('团长设置', request()->fullUrlWithQuery(['_t' => 6]));
                    //$tab->addLink('OEM账户', request()->fullUrlWithQuery(['_t' => 4]));
                    $tab->addLink('佣金设置', request()->fullUrlWithQuery(['_t' => 7]));
                    $tab->addLink('用户协议/隐私政策', request()->fullUrlWithQuery(['_t' => 5]));
                    break;
            }

            $row->column(12, $tab->withCard());
        });

        return $content
            ->header('基础配置');
    }

    protected function basic()
    {
        $form = new SettingForm($this->setting);
        $form->action(request()->fullUrl());

        $form->text('media_id', '广告媒体ID')->required();
        $form->decimal('max_ecpm', 'ECPM限额')
            ->help('超过最大限额后，广告收益不记录。(设置成 0 为不限制)')
            ->required();
        $form->divider();
        $form->number('ip_limit', '同IP最大用户数')->default(3);
        $form->divider();
        $form->text('app_name', 'APP名称')
            ->help('展示给用户可见')
            ->required();
        $form->decimal('app_code');
        $form->text('app_version');
        $form->divider();
        $form->text('hot_update_url', '热更新地址');
        $form->text('app_id');
        $form->text('app_secret');
        $form->text('qq_key', 'QQ群Key');
        $form->divider();
        $form->text('invites.download_app_url', 'APP下载地址')
            ->help('推广下载页面优先使用填写的APP下载地址');

        $form->file('invites.local_download_url', '上传APP到本地')
            ->maxSize(1024 * 60)
            ->autoUpload()->url(url('filebase/upload'));

        $form->divider();
        $form->text('player_group_url', '玩家微信群链接');
        $form->image('player_group_qrcode', '玩家群微信二维码')
            ->autoUpload()->url(url('filebase/upload'));
        $form->text('service_url', '客服微信');
        $form->image('service_qrcode', '客服微信二维码')
            ->autoUpload()->url(url('filebase/upload'));
        $form->image('app_logo', 'APPlogo')
            ->autoUpload()->url(url('filebase/upload'));
        $form->divider();

        return $form->render();
    }

    public function withdraws()
    {
        $form = new SettingForm($this->setting);


        $form->decimal('exchange_rate', '1元兑换金币数量')->placeholder(10000)->required();
        $form->decimal('max_redpack_amount', '领取红包最大值')->required();
        $form->decimal('withdraw_limit', '提现审核金额')
            ->required()
            ->help('低于该值自动过审核(单位元)');
        $form->decimal('withdraw_count_per_day', '用户每天提现总次数')
            ->required()
            ->help('设置成 0 为不限制');
        $form->divider();
        $form->textarea('withdrawsinfo', '提现注意')->rows(10);
        $form->divider();
        // extra[earnings][value]
        $form->table('withdrawals', function (NestedForm $table) {
            $table->hidden('id')->readOnly();
            $table->decimal('value', '金额')->required();
            $table->text('label', '标签');
            $table->decimal('limit', '每日可提现次数');
        })->label('提现金额配置');
        $form->text('mch_id', '商户id');
        return $form->render();
    }

    public function invites()
    {
        $form = new SettingForm($this->setting);
        $form->text('coin_starter', '首次红包奖励')->required()
            ->help("新用户注册首次红包奖励后的金币数量，请填写整数数字！");
        $form->divider();
        $form->text('invites.minimum_money', '最低激励广告金币')
            ->help("用户观看激励广告兜底金币数量，请填写整数数字！没有填0");
        $form->text('invites.ulinkid', '友盟U-Link ID')->required();
        $form->text('invites.appkey', '友盟AppKey')->required();
        $form->divider();
        $form->decimal('earnings.max_per_guest', '每邀请用户最大收益')->placeholder(200)
            ->required()
            ->help('单位：元');

        $form->table('earnings.b_to_a_ratios', function (NestedForm $table) {
            $table->decimal('b_value', 'B提现金额')->required();
            $table->text('a_value', 'A得佣金');
            $table->hidden('condition')->value('=');
            $table->decimal('ratio', '佣金比例');
        })->label('师徒奖励配置');
        $form->divider('说明：若「A得佣金」为空则：A得佣金 = B提现金额 * 佣金比例');


        return $form->render();
    }

    public function group()
    {
        $form = new SettingForm($this->setting);
        $form->switch('group.enabled', '团长功能启用')
            ->help('全局团长功能开关：关闭后团长相关功能将暂停');
//        $form->display('group_entry', '团长H5登录页面');
        $form->decimal('group_sub_rate', '下级用户购买高级团长返现')
            ->default(0)
            ->help('团长下级用户购买成为团长后返给直接上级团长的佣金【填写值 小于 1: 佣金 = 开团长金额 * 比例；填写值 大于 1: 佣金就是固定填写的值；0 为不进行返现】');
        $form->text('group.price', '开通高级团长价格')->help('单位：元；开通高级支付金额必须大于 0.01！')->required();
        $form->text('group.lowest_member', '开通普通团长最低人数')
            ->help('填 0 时用户不会自动变成普通团长')
            ->required();
        $form->text('group.advanced_lowest_member', '开通高级团长最低人数')
            ->help('填 0 时用户不会自动变成高级团长')
            ->required();
//        $form->text('group.withdrawable', '团长最低提现金额')
//            ->required();
//        $form->text('group.invite_prize', '成员收益比例')
//            ->required()
//            ->help("取值范围：0 ~ 1.0");
//        $form->text('group.direct_prize', '专属收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.return_days', '返现天数')
//            ->help("取值范围：大于0");
//        $form->text('group.return_amount', '每条返现价格')
//            ->help("取值范围：大于0,单位元");
//
//        $form->text('group.son_prize', '子团队收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('advanced_group.advanced_invite_prize', '高级团长成员收益比例')
//            ->required()
//            ->help("取值范围：0 ~ 1.0");
//        $form->text('advanced_group.advanced_direct_prize', '高级团长专属收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('advanced_group.advanced_son_prize', '高级团长子团队收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
        return $form->render();
    }

    public function commissions()
    {
        $form = new SettingForm($this->setting);
        $form->divider('普通团长');
        $form->text('group.commissions2_1', '普通团长1级')
            ->help("取值范围：0 ~ 1.0，一级普通团员提现普通团长抽佣比例，二级普通团员提现一级普通团员抽佣比例")
            ->required();
        $form->text('group.commissions2_2', '普通团长2级')
            ->help("取值范围：0 ~ 1.0，二级普通团员提现普通团长抽佣比例")
            ->required();
        $form->divider('高级团长');

        $form->text('group.commissions3_1', '高级团长1级')
            ->help("取值范围：0 ~ 1.0，一级高级团员提现高级团长抽佣比例，二级高级团员提现一级团员抽佣比例，，三级高级团员提现一级团员抽佣比例")
            ->required();
        $form->text('group.commissions3_2', '高级团长2级')
            ->help("取值范围：0 ~ 1.0，二级高级团员提现高级团长抽佣比例，三级高级团员提现二级团员抽佣比例")
            ->required();
        $form->text('group.commissions3_3', '高级团长3级')
            ->help("取值范围：0 ~ 1.0，三级高级团员提现高级团长抽佣比例")
            ->required();


//        $form->text('group.commissions7_1', '1.普通用户直推用户收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_2', '2.普通用户直推普通团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_3', '3.普通用户直推高级团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_4', '4.普通用户间推用户收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_5', '5.普通用户间推普通团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_6', '6.普通用户间推高级团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->divider();
//        $form->text('group.commissions7_7', '7.普通团长直推普通团员1级收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_8', '8.普通团长直推普通团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_9', '9.普通团长直推高级团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_10', '10.普通团长间推普通团员2级收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_11', '11.普通团长间推普通团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_12', '12.普通团长间推高级团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->divider();
//        $form->text('group.commissions7_13', '13.普通团员1级直推普通团员2级收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_14', '14.普通团员1级直推普通团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_15', '15.普通团员1级直推高级团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_16', '16.普通团员1级间推用户收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_17', '17.普通团员1级间推普通团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_18', '18.普通团员1级间推高级团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->divider();
//        $form->text('group.commissions7_19', '19.普通团员2级直推用户收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_20', '20.普通团员2级直推普通团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_21', '21.普通团员2级直推高级团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_22', '22.普通团员2级间推用户收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_23', '23.普通团员2级间推普通团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_24', '24.普通团员2级间推高级团长收益比例')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->divider();
//        $form->text('group.commissions7_25', '25.高级团长直推普通团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_26', '26.高级团长直推高级团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_27', '27.高级团长直推高级团员1级')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_28', '28.高级团长间推高级团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_29', '29.高级团长间推普通团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_30', '30.高级团长间推高级团员2级')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->divider();
//        $form->text('group.commissions7_31', '31.高级团员1级直推普通团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_32', '32.高级团员1级直推高级团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_33', '33.高级团员1级直推高级团员2级')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_34', '34.高级团员1级间推用户1')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_35', '35.高级团员1级间推普通团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_36', '36.高级团员1级间推高级团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->divider();
//        $form->text('group.commissions7_37', '37.高级团员2级直推用户')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_38', '38.高级团员2级直推高级团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_39', '39.高级团员2级直推普通团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_40', '40.高级团员2级间推用户')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_41', '41.高级团员2级间推高级团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->text('group.commissions7_42', '42.高级团员2级间推普通团长')
//            ->help("取值范围：0 ~ 1.0")
//            ->required();
//        $form->divider();
        return $form->render();
    }

    public function payments()
    {
        $form = new SettingForm($this->setting);
//        $form->divider('KylinCloud 零钱转账设置');
//        $form->text('payment.kylin.company_id', '企业ID')->required();
//        $form->text('payment.kylin.remark', '转账备注')
//            ->required()
//            ->help('按开发配置进行填写');
//        $form->text('payment.kylin.sign_type', '签名类型')->required()->help('固定RSA2');
//        $form->textarea('payment.kylin.private_key', '企业私钥匙')
//            ->required();

        $form->text('bank.account_id', '提现账户')->readOnly();
        $form->text('bank.balance', '账户余额')->readOnly();

        $form->text('whitelist', 'IP白名单')->help('请勿随意修改');


        return $form->render();
    }

    private function privices()
    {
        $data = [
            'privicy' => $this->setting['privicy'],
            'agreement' => $this->setting['agreement'],
        ];
        $form = new SettingForm($data);

        $form->editor('privicy', '隐私政策')->options([
            'toolbar' => false,
            'menubar' => true,
            'setup' => JavaScript::make('function (editor) {}'),
        ])->help(sprintf('访问 <a href="%s" target="_blank">%s</a> 进行查看', url('privicy'), url('privicy')));
        $form->divider();
        $form->editor('agreement', '用户协议')->options([
            'toolbar' => false,
            'menubar' => true,
            'setup' => JavaScript::make('function (editor) {}'),
        ])->help(sprintf('访问 <a href="%s" target="_blank">%s</a> 进行查看', url('agreement'), url('agreement')));

        return $form->render();
    }
}
